/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

using System;
using System.Linq;
using System.Collections.Generic;
using CookComputing.XmlRpc;

namespace Whitemice.ZOGI
{
  //TODO: Add IComparable interface so notations can be sorted by time
  public class TaskNotation : Entity, IZOGIStorable
  {
    MultiResultCallback actorCallback;
    
    public TaskNotation(int taskId, TaskAction action, string comment) : base()
    {
      SetParentObjectId(taskId);
      SetAction(action);
      SetComment(comment);
      SetDate(DateTime.Now);
    }
          
    public TaskNotation(IDictionary<string,object> x, int d)
      : base(x, d)
    {
    }
    
    internal override void Take(IDictionary<string,object> x, int d)
    {
      base.Take(x, d);
      
			SetComment(AsString(x, "comment"));
			SetAction(AsString(x, "action"));
			SetStatus(AsString(x, "taskStatus"));
			SetDate(AsDateTime(x, "actionDate").ToLocalTime());
			SetParentObjectId(AsInt32(x, "taskObjectId"));
      this.ZLOG(string.Format("TaskNotation#{0} marshalled, child of Task#{1}", ObjectId, ParentObjectId));
    }

    protected void RequestActor()
    {
      FetchCommand command;
      
      actorCallback = new MultiResultCallback(ReceiveActor);
      command = new FetchCommand(new int[] { ActorId }, 65535, "MyActor");
      command.Callback = actorCallback;
      Server.Connection.RunCommand(command, RpcPriority.Background);
    }
    
    protected void ReceiveActor(object sender, RPCEventArgs args)
		{
      if (args.HasResults)
		    SetActor(args.Payload.ElementAt(0));
		    else SetActor(null);
		} // end ReceiveOwner    

    protected DateTime _date;
    protected void SetDate(DateTime d) { _date = d; }
		public DateTime Date { get { return _date; } }

    protected string _status;
    protected void SetStatus(string s) { _status = s; }
		public TaskStatus TaskStatus
		{
 			get { return Task.StringToStatus(_status); }
		}

		public int TaskId { get { return ParentObjectId; } }
		
		protected string _action;
		protected void SetAction(string a) { _action = a; }
		protected void SetAction(TaskAction a) 
		{ 
		  SetAction(Task.ActionToString(a));
		}
		public TaskAction Action
		{
 			get { return Task.StringToAction(_action); }
		}
		
		protected int _actorId = 0;
    protected Entity _actor = null;
		protected void SetActorId(int a) 
    { 
      if (_actorId != a)
      {
        _actorId = a;
        RequestActor();
      }
    }
    protected void SetActor(Entity e) { _actor = e; }
		public int ActorId { get { return _actorId; } }
    public string ActorName
    {
      get
      {
        if (_actor == null)
          return string.Empty;
        if (_actor is Contact)
          return string.Format("{0}, {1}", (_actor as Contact).LastName,
                                           (_actor as Contact).FirstName);
        return _actor.ObjectId.ToString();
      }
    }
		
		protected string _comment;
		protected void SetComment(string c) { _comment = c; }
		public string Comment { get { return _comment; } }
		
		public override XmlRpcStruct Encode()
		{
			XmlRpcStruct	rpcStruct;
			
			if(ObjectId > 0) 
				return null;
				
		  rpcStruct = base.Encode();
			rpcStruct.Add("entityName", "taskNotation");
			rpcStruct.Add("taskObjectId", TaskId);
			rpcStruct.Add("objectId", 0);
			rpcStruct.Add("comment", Comment);
			rpcStruct.Add("action", Task.ActionToEncodeString(Action));
			return rpcStruct;	
    }	
  }
}
